package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.Reply;
import com.czh.springboot04.service.ReplyService;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reply")
public class ReplyController {
   @Autowired
   private ReplyService replyService;
   @PostMapping("/add")
   public R addReply(@RequestBody Reply reply){
       int i=replyService.addReply(reply);
       if(i>0){
           return R.ok(reply);
       }else {
           return R.fail("添加失败");
       }
   }
   @PutMapping("/update")
   public R updateReply(@RequestBody Reply reply){
       int i=replyService.updateReply(reply);
       if(i>0){
           return R.ok("修改成功");
       }else {
           return R.fail("失败");
       }
   }
   @DeleteMapping("/delete/{id}")
   public R deleteReply(@PathVariable("id")int id){
       int i=replyService.deleteReply(id);
       if(i>0){
           return R.ok("删除成功");
       }else {
           return R.fail("失败");
       }
   }
   @GetMapping("/findAll")
   public R findAll(@RequestParam(required = false,defaultValue = "1")int pageNum,
                    @RequestParam(required = false,defaultValue = "3")int pageSize,Reply reply){
       PageInfo<Reply> pageInfo=replyService.findAll(pageNum,pageSize,reply);
       return R.ok(pageInfo);
   }
   @GetMapping("/findByQid/{id}")
       public R findByQid(@PathVariable("id") int questionId){
       List<Reply> replies=replyService.findById(questionId);
       return R.ok(replies);
   }
}
